package com.liang.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.mall.entity.Category;
import com.liang.mall.entity.Menu;
import com.liang.mapper.CategoryMapper;
import com.liang.service.CategoryService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author liang
 * @since 2021-05-17
 */
@Service
//@AllArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
  private  CategoryMapper categoryMapper;


    @Override
    public List<Category> getAllCatagory() {

        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<>());

        if (CollectionUtils.isEmpty(categoryList)) {
            return null;
        }

        Map<Long, List<Category>> collect = categoryList.stream().collect(Collectors.groupingBy(category -> category.getParentId()));

        for (Map.Entry<Long, List<Category>> longListEntry : collect.entrySet()) {

            Long key = longListEntry.getKey();  //父级id
            List<Category> value = longListEntry.getValue();  //集合数据

            for (Category category : categoryList) {
                if (category.getCategoryId() == key)
                    category.setChildren(value);
            }

        }

        return collect.get(0L);

    }
}
